package cn.gp.chat.mapper;

import cn.gp.chat.commom.resp.MessageVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageMapper {
    @Insert("INSERT INTO chat_messages (sender_id, receiver_id, content_type, content, status) " +
            "VALUES (#{senderId}, #{receiverId}, #{contentType}, #{content}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MessageVo messageVo);

    /**
     * @Param userId
     * @Param contactId
     * @Param limit
     * @Return java.util.List<cn.gp.chat.commom.resp.MessageVo>
     * @Description 字段不一致 数据库是下划线 这里是驼峰 需要起别名或者通过xml来映射
     * @Author He Guoping
     * @Date 2025/6/19 9:57
     */
    @Select("SELECT " +
            "id, " +
            "sender_id as senderId, " +
            "receiver_id as receiverId, " +
            "content_type as contentType, " +
            "content, " +
            "file_url as fileUrl, " +
            "status, " +
            "create_time as createTime, " +
            "update_time as updateTime " +
            "FROM chat_messages " +
            "WHERE (sender_id = #{userId} AND receiver_id = #{contactId}) " +
            "   OR (sender_id = #{contactId} AND receiver_id = #{userId}) " +
            "ORDER BY create_time DESC " +
            "LIMIT #{limit}")
    List<MessageVo> getChatHistory(@Param("userId") Long userId, 
                               @Param("contactId") Long contactId, 
                               @Param("limit") int limit);

    @Select("SELECT " +
            "id, " +
            "sender_id as senderId, " +
            "receiver_id as receiverId, " +
            "content_type as contentType, " +
            "content, " +
            "file_url as fileUrl, " +
            "status, " +
            "create_time as createTime, " +
            "update_time as updateTime " +
            "FROM chat_messages WHERE id = #{id}")
    MessageVo getMessageById(@Param("id") Long id);

    @Select("SELECT COUNT(*) FROM friendships " +
            "WHERE ((user_id = #{userId1} AND friend_id = #{userId2}) " +
            "OR (user_id = #{userId2} AND friend_id = #{userId1})) " +
            "AND status = 1")
    int checkFriendship(Long userId1, Long userId2);
} 